<template>
  <a-upload
    v-model:file-list="fileList"
    name="file"
    :multiple="false"
    action="http://base.uat.iplus.chanjue.top/saas-filesvr-web/servlet/jqueryFileUpload"
    :headers="headers"
    :showUploadList=false
    @change="handleChange"
    :before-upload="beforeUpload"
  >
    <a-button>
      <upload-outlined></upload-outlined>
      上传  
    </a-button>
  </a-upload>
</template>
<script lang="ts">
import { message } from 'ant-design-vue';
import { UploadOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, getCurrentInstance, computed, unref } from 'vue';

interface FileItem {
  uid: string;
  name?: string;
  status?: string;
  response?: string;
  url?: string;
}

interface FileInfo {
  file: FileItem;
  fileList: FileItem[];
}

export default defineComponent({
  emits: {
   uploadFiles: (file) => {
     return /http/.test(file)
   }
  },
  components: {
    UploadOutlined,
  },
 
  setup(_, ctx) {
    const handleChange = (info: FileInfo) => {
      if (info.file.status !== 'uploading') {
        ctx.emit('uploadFiles', info.fileList[info.fileList.length - 1].response.files[0].url)
        console.log(info.fileList[info.fileList.length - 1].response.files[0].url);
      }
      if (info.file.status === 'done') {
        message.success('上传成功!~');
      } else if (info.file.status === 'error') {
        message.error('上传失!~');
      }
    };
    const beforeUpload = () => {
      // 手动上传为false
      return true
    }
    const fileList = ref([]);
    return {
      fileList,
      headers: {
        authorization: 'authorization-text',
      },
      handleChange,
      beforeUpload
    };
  },
});
</script>